<?php


/**
 * 获得商品类型的列表
 *
 * @access  public
 * @param   integer     $selected   选定的类型编号
 * @return  string
 */
function goods_type_list($selected)
{
    $sql = 'SELECT cat_id, cat_name FROM ' . 'ybr_goods_type' . ' WHERE enabled = 1';
    $res = M()->query($sql);
    $list = '';
    foreach($res as $row){
    	$list .= "<option value='$row[cat_id]'";
        $list .= ($selected == $row['cat_id']) ? ' selected="true"' : '';
        $list .= '>' . htmlspecialchars($row['cat_name']). '</option>';
    }
    return $list;
}

/**
 * 获取属性列表
 *
 * @return  array
 */
function get_attrlist()
{
    /* 查询条件 */
    $filter = array();
    $filter['goods_type'] = empty($_REQUEST['goods_type']) ? 0 : intval($_REQUEST['goods_type']);
    $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'sort_order' : trim($_REQUEST['sort_by']);
    $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

    $where 	= 	(!empty($filter['goods_type'])) ? " WHERE a.cat_id = '$filter[goods_type]' " : '';

    $sql 	= 	"SELECT COUNT(*) FROM " . 'ybr_attribute' . " AS a $where";
    
    /* 查询 */
    $sql 	= 	"SELECT a.*, t.cat_name " .
	            " FROM " . 'ybr_attribute' . " AS a ".
	            " LEFT JOIN " . 'ybr_goods_type' . " AS t ON a.cat_id = t.cat_id " . $where .
	            " ORDER BY $filter[sort_by] $filter[sort_order] ";
    $row 	= 	M()->query($sql);
	
    /*属性录入方式*/
    $value_attr_input_type_array[0]	=	'手工录入';
    $value_attr_input_type_array[1]	=	'从列表中选择';
    $value_attr_input_type_array[2]	=	'多行文本框';
        
    foreach ($row AS $key => $val){
        $row[$key]['attr_input_type_desc'] 	= 	$value_attr_input_type_array[$val['attr_input_type']];
        $row[$key]['attr_values']      		= 	str_replace("\n", ", ", $val['attr_values']);
    }
	return $row;
}

/**
 * 获得指定的商品类型下所有的属性分组
 *
 * @param   integer     $cat_id     商品类型ID
 *
 * @return  array
 */
function get_attr_groups($cat_id){	
	$CommonModel	=	new CommonModel();
    $sql 			= 	"SELECT attr_group FROM " . 'ybr_goods_type' . " WHERE cat_id='$cat_id'";
    $grp 			= 	str_replace("\r", '', $CommonModel->getOne($sql));
    
	/*属性组*/
    if ($grp){
        return explode("\n", $grp);
    }
    else{
        return array();
    }
}




